package bob.project.slick;


import java.io.File;
import java.util.ArrayList;
import java.util.Random;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.MouseOverArea;
import org.newdawn.slick.imageout.ImageOut;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import bob.project.ResourceManager;
import bob.project.inventory.Item;
import bob.project.inventory.ItemManager;
import bob.project.object.Cloud;
import bob.project.object.GameObject;
import bob.project.object.ItemObject;
import bob.project.object.Player;
import bob.project.object.StaticObject;
import bob.project.object.enemyes.Smit;
import bob.project.object.enemyes.Zombie;

public class GameState extends BasicGameState implements ComponentListener {

	public static int ALIGN_LEFT = 0;
	public static int ALIGN_CENTER = 1;
	public static int ALIGN_RIGHT = 2;
	
	public static Random rnd = new Random();
	public static GameState instance;
	public ArrayList<GameObject> objects = new ArrayList<GameObject>();
	MouseOverArea weapon_hands;
	MouseOverArea weapon_shotgun;
	MouseOverArea weapon_smg;
	public Player player;
	private Image hud;
	private Image life;
	private Image energy;
	private Image hp;
	private Image sky;
	public boolean wasd[] = new boolean[5];
	public boolean inventoryOpened = false;
	
	public int offsetX,offsetY;
	GameContainer gc;
	public static UnicodeFont font_12;
	
	@Override
	public void init(GameContainer arg0, StateBasedGame arg1)
			throws SlickException
	{
		gc = arg0;
		ResourceManager.loadResources(ResourceManager.class.getResourceAsStream("resources.xml"));
		ItemManager.init();
		Color onOver = new Color(Color.lightGray.getRed(),Color.lightGray.getGreen(),Color.lightGray.getBlue(),0.4f);
		Color onDown = new Color(Color.white.getRed(),Color.white.getGreen(),Color.white.getBlue(),0.4f);
		weapon_hands = new MouseOverArea(arg0, ResourceManager.getImage("icon_weapon_hands"), 320, 32, this);
		weapon_hands.setMouseOverColor(onOver);
		weapon_hands.setMouseDownColor(onDown);
		weapon_shotgun = new MouseOverArea(arg0, ResourceManager.getImage("icon_weapon_shotgun"), 370, 32, this);
		weapon_shotgun.setMouseOverColor(onOver);
		weapon_shotgun.setMouseDownColor(onDown);
		weapon_smg = new MouseOverArea(arg0, ResourceManager.getImage("icon_weapon_smg"), 420, 32, this);
		weapon_smg.setMouseOverColor(onOver);
		weapon_smg.setMouseDownColor(onDown);
		
		font_12 = new UnicodeFont(new java.awt.Font("Tahoma", java.awt.Font.PLAIN, 12));
		font_12.getEffects().add(new ColorEffect(java.awt.Color.white)); 
		//font.getEffects().add(new ShadowEffect(java.awt.Color.red,2,2,0.3f)); 
		font_12.addAsciiGlyphs(); 
		font_12.addGlyphs(0x400,0x455);
		font_12.loadGlyphs();
		
		sky = ResourceManager.getImage("sky");
		hud = ResourceManager.getImage("hud");
		life = ResourceManager.getImage("life");
		Image layer = ResourceManager.getImage("layer");
		hp = layer.getSubImage(0, 0, 7, 12);
		energy = layer.getSubImage(0, 12, 7, 12);
		player = new Player();
		objects.add(player);
		StaticObject obj = new StaticObject();
		obj.setDimensions(-1200, 400, 400, 200);
		objects.add(obj);
		
		obj = new StaticObject();
		obj.setDimensions(-800, 430, 400, 200);
		objects.add(obj);
		
		obj = new StaticObject();
		obj.setDimensions(-400, 460, 400, 200);
		objects.add(obj);
		
		obj = new StaticObject();
		obj.setDimensions(0, 490, 400, 200);
		objects.add(obj);
		
		obj = new StaticObject();
		obj.setDimensions(400, 450, 400, 200);
		objects.add(obj);
		
		obj = new StaticObject();
		obj.setDimensions(800, 430, 400, 200);
		objects.add(obj);

		Zombie zombie  = new Zombie();
		zombie.setDimensions(500, 100, 24, 46);
		objects.add(zombie);

		
		for(int i = 0; i < 6; i++)
		{
			Cloud cloud = new Cloud();
			cloud.setDimensions(-64, rnd.nextInt(300), 32, 32);
			objects.add(cloud);
			cloud.image = "cloud_"+rnd.nextInt(3);
		}
	}

	@Override
	public void render(GameContainer arg0, StateBasedGame arg1, Graphics g)
			throws SlickException
	{
		sky.draw(0, 0, arg0.getWidth(), arg0.getHeight());
		g.translate(offsetX, offsetY);
		for(GameObject obj : objects)
			obj.draw(g);
		//player.draw(g);
		g.resetTransform();
		
		drawGUI(arg0,g);
	}

	private void drawGUI(GameContainer arg0,Graphics g)
	{
		hud.draw(32, 32);
		if (player != null)
		{
			for(int i = 0; i < player.lives;i++)
				life.draw(32+132+i*16, 38);
			
			hp.draw(32+132, 55, player.hp/(player.maxHp/100)*0.01f*40, 12);
			
			energy.draw(32+132, 55+12, player.energy*0.01f*40, 12);
		}
		weapon_hands.render(arg0, g);
		weapon_shotgun.render(arg0, g);
		if (player.inv.shotgun != null)
		{
			int current = player.inv.shotgun.ammoCountLoaded;
			int total = 0;
			if ( player.inv.shotgun.ammo != null)
				total = player.inv.shotgun.ammo.count;
			GameState.drawString(370, 32+40+4, 370+40, -1,  current + " / "+ total, GameState.ALIGN_CENTER, font_12);
		}
		weapon_smg.render(arg0, g);
		if (player.inv.smg != null)
		{
			int current = player.inv.smg.ammoCountLoaded;
			int total = 0;
			if ( player.inv.smg.ammo != null)
				total = player.inv.smg.ammo.count;
			GameState.drawString(420, 32+40+4, 420+40, -1,  current + " / "+ total, GameState.ALIGN_CENTER, font_12);
		}
		if (inventoryOpened)
			player.inv.render(g);
		
	}

	long time = System.currentTimeMillis();
	long time2 = System.currentTimeMillis();
	
	@Override
	public void update(GameContainer arg0, StateBasedGame arg1, int delta)
			throws SlickException
	{
		for(int i = 0; i < objects.size();i++)
		{
			GameObject obj = objects.get(i);
			obj.update(delta);
			if (obj.removed)
				objects.remove(obj);
		}
		//player.update(delta);
		player.inv.update(delta);
		
		if (time < System.currentTimeMillis())
		{
			time = System.currentTimeMillis() + 3000L;
			Item it = ItemManager.createItemById(1+rnd.nextInt(ItemManager.items.size()));
			if (it != null)
			{
				ItemObject item = new ItemObject(it);
				item.y = 20;
				item.x = 20+rnd.nextInt(getGc().getWidth()-10);
				objects.add(item);
			}
		}

		if (time2 < System.currentTimeMillis())
		{
			time2 = System.currentTimeMillis() + 5000L;
			Zombie zombie = new Zombie();
			zombie.setDimensions(-1200 + rnd.nextInt(2400), 100, 24, 46);
			
			Smit xx = new Smit();
			xx.setDimensions(-1200 + rnd.nextInt(2400), 100, 34, 44);
			
			objects.add(xx);
		}		
		
		offsetX = -(int) (player.x - getGc().getWidth()/2); 

	}


	@Override
	public int getID()
	{
		return 0;
	}
	
	public void keyPressed(int key, char c)
	{
		if (inventoryOpened)
		{
			player.inv.keyPressed(key,c);
			return;
		}
		if (key == Input.KEY_W)	
			wasd[0] = true;
		if (key == Input.KEY_A)	
			wasd[1] = true;
		if (key == Input.KEY_S)	
			wasd[2] = true;
		if (key == Input.KEY_D)	
			wasd[3] = true;
		if (key == Input.KEY_SPACE)	
			wasd[4] = true;

	}

	public void keyReleased(int key, char c)
	{
		if (key == Input.KEY_TAB)
			inventoryOpened = !inventoryOpened;
	
		if (inventoryOpened)
		{
			player.inv.keyReleased(key,c);
			return;
		}
		if (key == Input.KEY_W)	
			wasd[0] = false;
		if (key == Input.KEY_A)	
			wasd[1] = false;
		if (key == Input.KEY_S)	
			wasd[2] = false;
		if (key == Input.KEY_D)	
			wasd[3] = false;
		if (key == Input.KEY_SPACE)	
			wasd[4] = false;
		if (key == Input.KEY_F10)
		{
			Image target;
			try {
				target = new Image(getGc().getWidth(), getGc().getHeight());

			getGc().getGraphics().copyArea(target, 0, 0);
			String name = "screen.png";
			int i = 0;
			File f = new File(name);
			while (f.exists())
			{
				
				name = "screen" + i + ".png";
				f = new File(name);
				i++;
			}
			
			ImageOut.write(target.getFlippedCopy(false, false), name, false);
			target.destroy();
			} catch (SlickException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		if (key == Input.KEY_1)	
		{
			if (player.currentWeapon == 1)
				player.currentWeapon = 0;
			else
				player.currentWeapon = 1;
		}
		if (key == Input.KEY_2)	
		{
			if (player.currentWeapon == 2)
				player.currentWeapon = 0;
			else
			{
				if (player.inv.shotgun != null)
					player.currentWeapon = 2;
			}
		}
		if (key == Input.KEY_3)	
		{
			if (player.currentWeapon == 3)
				player.currentWeapon = 0;
			else
			{
				if (player.inv.smg != null)
					player.currentWeapon = 3;
			}
		}
	}
	
	public GameContainer getGc()
	{
		return gc;
	}

	public static void drawString(int x,int y,int x1,int y1,String string,int align,org.newdawn.slick.Font font)
	{
		switch(align)
		{
			case 0:
			{
				font.drawString(x, y, string);
			}
			break;
			case 1:
			{
				int diffX = x1 - x;
				font.drawString(x+(diffX-font.getWidth(string))/2, y, string);
				
			}
			break;
			case 2:
			{
				font.drawString(x-font.getWidth(string), y, string);
			}
			break;
		
		}
	}
	
	public static void drawString(int x,int y,int x1,int y1,String string,int align,org.newdawn.slick.Font font,Color color)
	{
		switch(align)
		{
			case 0:
			{
				font.drawString(x, y, string,color);
			}
			break;
			case 1:
			{
				int diffX = x1 - x;
				font.drawString(x+(diffX-font.getWidth(string))/2, y, string,color);
				
			}
			break;
			case 2:
			{
				font.drawString(x-font.getWidth(string), y, string,color);
			}
			break;
		
		}
	}
	
	@Override
	public void componentActivated(AbstractComponent arg0)
	{
		if (arg0 == weapon_hands)
		{
			if (player.currentWeapon == 1)
				player.currentWeapon = 0;
			else
				player.currentWeapon = 1;
		}
		if (arg0 == weapon_shotgun)
		{
			if (player.currentWeapon == 2)
				player.currentWeapon = 0;
			else
			{
				if (player.inv.shotgun != null)
					player.currentWeapon = 2;
			}
		}
		if (arg0 == weapon_smg)
		{
			if (player.currentWeapon == 3)
				player.currentWeapon = 0;
			else
			{
				if (player.inv.smg != null)
					player.currentWeapon = 3;
			}
		}
		// TODO Auto-generated method stub
		
	}

}
